<?php


namespace app\admin\controller\server;

use app\common\controller\Adminbase;
use think\Db;

class Server extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('server')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    public function setstate2()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('server_price')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    public function getc_cate()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function index()
    {
        if ($this->request->isAjax()) {

            $filter = input('filter');
            list($page, $limit) = $this->buildTableParames();
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['cate_name'])) {
                    $where[] = ['s.cate_id', '=',  $filter['cate_name'] ];
                }
                if (isset($filter['name'])) {
                    $where[] = ['s.name', 'like',  '%'.$filter['name'].'%' ];
                }
                if (isset($filter['status'])) {
                    $where[] = ['s.status', '=',  $filter['status'] ];
                }

                if (isset($filter['dispatch_type'])) {
                    $where[] = ['s.dispatch_type', '=',  $filter['dispatch_type'] ];
                }

                if (isset($filter['server_type'])) {
                    $where[] = ['s.server_type', '=',  $filter['server_type'] ];
                }
            }

            $where[] = ['s.is_del', '=', 0];
            $_list = Db::name('server')->alias('s')
                ->where($where)
                ->page($page, $limit)
                ->order('s.sort desc')
                ->select();
            $total = Db::name('server')->alias('s')

                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }

        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = [
                'cate_id' => 1,
                'name' => $data['row']['name'],
                'price' => $data['row']['price'],
                'sort' => $data['row']['sort'],
                'desc' => $data['row']['desc'],
                'tag' => $data['row']['tag'],
                'icon_img' => $data['icon_img'],
                'bc_img' => $data['bc_img'],
                'detail' => $data['content'],
                'service_notice' => $data['service_notice'],
                'dispatch_type' => $data['row']['dispatch_type'],
                'server_type' => $data['row']['server_type'],
            ];
            $stmt = Db::name('server')->insertGetId($insert_data);
            $this->success('操作成功！');
        }

        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $update_data = [
                'id' => $data['id'],
                'name' => $data['row']['name'],
                'price' => $data['row']['price'],
                'tag' => $data['row']['tag'],
                'sort' => $data['row']['sort'],
                'desc' => $data['row']['desc'],
                'icon_img' => $data['icon_img'],
                'bc_img' => $data['bc_img'],
                'detail' => $data['content'],
                'service_notice' => $data['service_notice'],
                'dispatch_type' => $data['row']['dispatch_type'],
                'server_type' => $data['row']['server_type'],
            ];
            Db::name('server')
                ->update($update_data);
            $this->success('操作成功！');
        }

        $data = Db::name('server')->find($id);

        $this->assign('data', $data);
        return $this->fetch();
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('server')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function areap()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
            $filter = input('filter');
            list($page, $limit) = $this->buildTableParames();
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['status', '=',  $filter['status'] ];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $where[] = ['province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $where[] = ['city', '=', $city['name']];
                }
            }
            $where[] = ['is_del', '=', 0];
            $where[] = ['server_id', '=', $id];
            $_list = Db::name('server_price')
                ->where($where)
                ->page($page, $limit)
                ->order('province_id')
                ->order('city_id')
                ->select();
            $total = Db::name('server_price')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list=Db::name('area')->where([
            ['id','<>',0],
            ['pid','=',0]
        ])->select();
        foreach ($area_list as $k=>$v){
            $proviec[$v['id']]=$v['name'];
        }
        $this->assign('proviec',$proviec);
        $this->assign('server_id',$id);
        return $this->fetch();
    }
    public function addareap(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $province=Db::name('area')->find( $data['province']);
            $city=Db::name('area')->find( $data['city']);

            $where[]=['is_del','=',0];
            $where[]=['province_id','=',$data['province']];
            $where[]=['city_id','=',$data['city']];
            $where[]=['server_id','=',$data['server_id']];
            $is_set=Db::name('server_price')->where($where)->find();
            if ($is_set){
                $this->error('当前地区已经设置价格');
            }
            $insert_data = [
                'province' => $province['name'],
                'city' => $city['name'],
                'province_id' => $data['province'],
                'city_id' => $data['city'],
                'price'=>$data['price'],
                'create_time'=>time(),
                'server_id'=>$data['server_id']
            ];
            $stmt = Db::name('server_price')->insertGetId($insert_data);
            $this->success('操作成功！');
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $param = $this->request->param();
        $this->assign('server_id',$param['server_id']);
        $this->assign('proviec',$area_list);
        return $this->fetch();
    }

    public function editareap()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $province=Db::name('area')->find( $data['province']);
            $city=Db::name('area')->find( $data['city']);
            $update_data = [
                'id' => $data['id'],
                'province' => $province['name'],
                'city' => $city['name'],
                'province_id' => $data['province'],
                'city_id' => $data['city'],
                'price'=>$data['price'],
                'create_time'=>time(),
                'server_id'=>$data['server_id']
            ];
            $stmt = Db::name('server_price')->update($update_data);
            $this->success('操作成功！');
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $data=Db::name('server_price')->find($id);
        $city_list=Db::name('area')->where([
            'pid'=>$data['province_id']
        ])->select();
        $this->assign([
            'proviec'=>  $area_list,
            'city_list'=>$city_list,
            'data'=>$data
        ]);
        return $this->fetch();
    }

    //删除
    public function delareap()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('server_price')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

}